package com.beeweeb.services.tps.samples;

import com.beeweeb.services.tps.JobPool;
import com.beeweeb.services.tps.JobExecutor;
import com.beeweeb.services.tps.JobPoolExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author guido d'albore
 */
public class BasicJobSample {
    Logger logger = LoggerFactory.getLogger("ThreadPoolScheduler");

    @JobPoolExecutor
    public void executePool(JobPool s) {
        logger.info("executePool() called with parameter '{}'.", s);
        for(int i = 0; i < 250; i++) {
            s.pushJob(BasicJobSample.class, "Job#" + i);
        }
    }

    @JobExecutor
    public void executeJob(String o) {
        logger.info("executeJob() called with parameter '{}'.", o.toString());

        try {
            if(Math.random() > 0.05f) {
                Thread.sleep(1000L + (long)(Math.random()*2000f));
            } else {
                Thread.sleep(5000L + (long)(Math.random()*30000f));
            }
        } catch(Exception ex){
            logger.error("I cannot sleep.", ex);
        }
    }

}
